<?php

namespace App\Admin\Controllers\Project;

use App\Admin\Actions\Tools\TransverseTagBatchAction;
use App\Admin\Controllers\TransverseController;
use App\Extensions\ContentSingle;
use App\Extensions\LayerForm;
use App\Transverse;
use Encore\Admin\Admin;
use Encore\Admin\Form\NestedForm;
use Encore\Admin\Grid;

class TransverseCompleteController extends TransverseController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '横向课题-已结项';

    protected function header(Grid $grid)
    {
        //$grid->disableCreateButton();
        $grid->model()->where('state', 1);
        $this->service->header($grid);
    }

    protected function tools(Grid $grid)
    {
        $grid->tools(function (Grid\Tools $tools) {
            $tools->append(new  TransverseTagBatchAction());
        });
        $grid->hideColumns(['contract_no', 'buyer_unified_code', 'buyer_person', 'buyer_phone', 'buyer_address', 'review_status']);
    }


    public function complete($id, ContentSingle $content)
    {
        $model = Transverse::query()->findOrFail($id);
        $form = new LayerForm($model);
        $form->setActionCurrent();
//        $form->table('participants', '参加人员', function (NestedForm $table) {
//            $table->text('name', '姓名');
//            $table->text('dw', '单位');
//            $table->text('zc', '职称');
//            $table->text('rw', '承担任务');
//        });
        $form->html(view('admin.participants.table', [
            'list'   => $model->participants?:[],
            'column' => 'participants',
        ]), '参加人员');
        $form->date('end_time', '结项时间')->required();
        $form->hidden('state', '研究状态');
        $form->html('<div style="height: 100px"></div>', '');
        if (request()->isMethod('PUT')) {
            $form->hidden('participants');
            $form->saving(function (LayerForm $form) {
                $form->participants = collect($form->participants)->filter(function ($item) {
                    return !collect($item)->filter()->isEmpty();
                })->values()->keyBy(function ($item, $key) {
                    return sprintf('new_%s', $key + 1);
                })->toArray();
                $form->state = 1;
            });
            return $form->update($id);
        }
        Admin::style('#has-many-participants > table > thead > tr > th:nth-child(1){width:150px}');
        return $content->body($form->edit($id));
    }
}
